<!DOCTYPE html>
<html>
<script src="../../resources/js-test.js"></script>
<body>
<script id="myWorker" type="text/worker">
var setWidth = Math.pow(2, 31);
var setHeight = Math.pow(2, 31);

self.onmessage = function(e) {
  var canvas1 = new OffscreenCanvas(setWidth, setHeight);
  self.postMessage({version:'canvas1', width:canvas1.width, height:canvas1.height});

  var canvas2 = new OffscreenCanvas(null, null);
  self.postMessage({version:'canvas2', width:canvas2.width, height:canvas2.height});
};
</script>

<script>
jsTestIsAsync = true;
description("Tests that the OffscreenCanvas can handle invalid arguments on a worker");

var width;
var height;
var setWidth = Math.pow(2, 31);
var setHeight = Math.pow(2, 31);

function makeWorker(script) {
  var blob = new Blob([script]);
  return new Worker(URL.createObjectURL(blob));
}

function handleMessageFromWorker(msg) {
  width = msg.data.width;
  height = msg.data.height;
  switch (msg.data.version) {
    case 'canvas1':
      shouldBe('width', 'setWidth-1');
      shouldBe('height', 'setHeight-1');
      break;
    case 'canvas2':
      shouldBe('width', '0');
      shouldBe('height', '0');
      finishJSTest();
      break;
    default:
      testFailed("Unexpected failure");
      break;
  }
}

var worker = makeWorker(document.getElementById('myWorker').textContent);
worker.addEventListener('message', handleMessageFromWorker);
worker.postMessage("");
</script>

</body>
</html>
